import { Service } from 'user/services/service';
export type VerifyCodeType = 'reigster' | 'changeMobile';
import * as chitu from "maishu-chitu";
import { PermissionService } from 'maishu-services-sdk';
import { StationService } from './stationService';
export let userInfo = new chitu.ValueStore<UserInfo>();
export class MemberService extends Service {

    constructor() {
        super();
    }

    private url(path: string) {
        return `UserMember/${path}`;
    }

    userInfo(): Promise<UserInfo> {
        let url = `UserMember/User/CurrentUserInfo`;//this.url('User/CurrentUserInfo');
        return this.get<UserInfo>(url);
    }

    saveUserInfo(userInfo): Promise<any> {
        let url = this.url('Member/SaveUserInfo');
        return this.putByJson(url, userInfo);
    }

    sentVerifyCode(mobile: string, type: VerifyCodeType): Promise<{ smsId: string }> {
        console.assert(mobile != null);
        let url = `sms/sendVerifyCode`;
        return this.putByJson(url, { mobile, type });
    }

    checkVerifyCode(smsId: string, verifyCode: string) {
        let url = `sms/checkVerifyCode`;
        return this.getByJson(url, { smsId, verifyCode });
    }


    /** 发送验证码到指定的手机 */
    sentRegisterVerifyCode(mobile: string): Promise<{ SmsId: string }> {
        console.assert(mobile != null);
        // let url = `UserMember/SMS/SendRegisterVerifyCode`;
        let url = 'user'
        return this.postByJson(url, { mobile, type: 'register' });
        // return this.sentVerifyCode(mobile, 'reigster');
    }

    /** 用户注册 */
    async register(mobile: string, password: string, smsId: string, verifyCode: string) {
        // console.assert(data != null);
        // let url = 'user/register' //`UserMember/User/Register`;
        // let { mobile, password } = data;
        // let { smsId, verifyCode } = data;

        // let obj = { username: mobile, password, smsId, verifyCode };

        let ps = this.createService(PermissionService)
        let r = await ps.register(mobile, password, smsId, verifyCode)
        Service.loginInfo.value = r;
        // return this.postByJson<{ token: string }>(url, { mobile, password, smsId, verifyCode }).
        //     then((data) => {
        //         // tokens.userToken.value = data.token;
        //         Service.loginInfo.value = {
        //             token: data.token,
        //             userId: data
        //         }
        //         return data;
        //     });
    }

    async login(username: string, password: string): Promise<LoginResult> {
        // let url = `user/login`;
        // return this.postByJson<LoginResult>(url, { username, password }).then((result) => {
        //     tokens.userToken.value = result.token;
        //     return result;
        // });
        let ps = this.createService(PermissionService);
        let r = await ps.login(username, password);
        Service.loginInfo.value = r;
        return r;
    }

    resetPassword(mobile: string, password: string, smsId: string, verifyCode: string) {
        let url = `user/resetPassword`;
        return this.putByJson(url, { mobile, password, smsId, verifyCode }).then(data => {
            return data;
        });
    }

    changePassword(password: string, smsId: string, verifyCode: string) {
        let url = `user/changePassword`;
        return this.putByJson(url, { password, smsId, verifyCode }).then(data => {
            return data;
        });
    }

    changeMobile(mobile: string, smsId: string, verifyCode: string) {
        let url = `user/changeMobile`;
        return this.putByJson(url, { mobile, smsId, verifyCode });
    }

    private _store: Store;
    async store() {
        if (this._store) {
            return this._store;
        }
        let url = this.url("Application/Item")
        let store = await this.getByJson<Store>(url, { id: this.applicationId });
        store.Style = store.Style || 'default'
        this._store = store;
        return this._store;
    }
}

